package com.peng.utils.retrofit;

import java.io.IOException;
import java.util.Map;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.peng.utils.JacksonUtil;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

/**
 * RetrofitSample
 *
 * @author lupeng
 * Created on 2022-05-14
 */
public class RetrofitSample {

    private static final Logger logger = LoggerFactory.getLogger(RetrofitSample.class);

    private ApiService apiService;

    @BeforeEach
    public void init() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://localhost:8080")
                .addConverterFactory(JacksonConverterFactory.create(JacksonUtil.getObjectMapper()))
                .build();
        this.apiService = retrofit.create(ApiService.class);
    }

    @Test
    public void s1() throws IOException {
        Call<ResponseBody> responseBodyCall = apiService.hello("guoguo");
        Response<ResponseBody> execute = responseBodyCall.execute();
        System.out.println(execute.body());
    }
    
    @Test
    public void s2() throws IOException {
        Response<Map<String, Object>> response = apiService.helloWithJackson("lupeng").execute();
        Map<String, Object> body = response.body();
        System.out.println(body);
    }
}
